<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Iterable 可迭代对象</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.types.literal.html">« literal 类型</a></li>
      <li style="float: right;"><a href="language.types.declarations.html">类型声明 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.types.html">类型</a></li>
    <li>Iterable 可迭代对象</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.types.iterable" class="sect1">
 <h2 class="title">Iterable 可迭代对象</h2>

 <p class="para">
  <span class="type"><a href="language.types.iterable.html" class="type Iterable">Iterable</a></span> 是内置编译时 <code class="literal">array|Traversable</code>
  
  的类型别名。从 PHP 7.1.0 到 PHP 8.2.0 之间的描述来看，<span class="type"><a href="language.types.iterable.html" class="type iterable">iterable</a></span>
  是内置伪类型，充当上述类型别名，也可以用于类型声明。iterable 类型可用于 <a href="control-structures.foreach.html" class="link">foreach</a>
  或在<a href="language.generators.html" class="link">生成器</a>中使用 <strong class="command">yield from</strong>。
 </p>

 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
   将可迭代对象声明为返回类型的函数也可能是 <a href="language.generators.html" class="link">生成器</a>。

   <div class="example" id="example-73">
    <p><strong>示例 #1 
     可迭代生成器返回类型的示例
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">gen</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">iterable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </p></blockquote>
</div></div></div></body></html>